#include "CServiceRepository.h"
#include "CFileProxy.h"

int run_server(int port);
int run_client(int, char**);

void print_usage(char *prog)
{
	cerr << "Usage : " << prog << " [access point] [option] [option_value]" << endl;
	cerr << "\t\t-h host" << endl;
	cerr << "\t\t-p port" << endl;
	cerr << "\t\t-w file" << endl;
	cerr << "\t\t-r key" << endl;
	cerr << "\t\t-t key" << endl;
	cerr << "\t\t-l remote location to save file" << endl;
	cerr << endl;
	cerr << "(ex) file server(by default, repository will be /tmp)" << endl;
	cerr << "\t" << prog << " -p 18080" << endl;
	cerr << endl;
	cerr << "(ex) file server with config file(by user, repository will be different)" << endl;
	cerr << "\t" << prog << " -c cube.conf" << endl;
	cerr << endl;
	cerr << "(ex) file client" << endl;
	cerr << "\t" << prog << " -h localhost -p 18080 -w or  -r test.txt [-l /tmp]" << endl;
	cerr << endl;
	cerr << "(ex) file client with config file" << endl;
	cerr << "\t" << prog << " -c cube.conf -w or -r test.txt [-l /tmp]" << endl;
}

int run_server(char *conf)
{ 
	CServiceRepository *stream_service = new CServiceRepository(conf);

	stream_service->run();

	delete stream_service;

	return 1;
}

int run_server(int port)
{ 
	CServiceRepository *stream_service = new CServiceRepository(port);

	stream_service->run();

	delete stream_service;

	return 1;
}

int run_client(char *conf, int op, char *file, char *location)
{ 
	CProperty config(conf);

	pair<string,string> kv;
	kv.first = COORD_CONF_MASTER_IP_KEY;
	string host = config.get(kv).second;

	kv.first = COORD_CONF_MASTER_PORT_KEY;
	int port = atoi((char *)config.get(kv).second.c_str());

	CFileProxy *stream_proxy = new CFileProxy((char *)host.c_str(), port);
	stream_proxy->repository(location);
	
	if(op==0) stream_proxy->put(file);
	else if(op==1) stream_proxy->get(file);
	else if(op==2) cerr << "not yet" << endl; 

	delete stream_proxy;

	return 1;
}

int run_client(char *host, int port, int op, char *file, char *location)
{ 
	CFileProxy *stream_proxy = new CFileProxy(host, port);
	stream_proxy->repository(location);

	if(op==0) stream_proxy->put(file);
	else if(op==1) stream_proxy->get(file);
	else if(op==2) cerr << "not yet" << endl; 

	delete stream_proxy;

	return 1;
}

int main(int argc, char **argv)
{ 
	if(argc<2) print_usage(argv[0]);

	int opt;
	static char optstr[] = "chpwrtdl";

	char *conf = NULL;
	char *host = NULL;
	int port = -1;
	char *file = NULL;
	int op = -1;
	char *location = "/tmp";

	while((opt=getopt(argc,argv,optstr))!=-1) {
		switch(opt) {
			case 'c' :
				conf = argv[optind++];
				break;
			case 'h' :
				host = argv[optind++];
				break;
			case 'p' :
				port = atoi(argv[optind++]);
				break;
			case 'w' :
				op = 0;
				file = argv[optind++];
				break;
			case 'r' :
				op = 1;
				file = argv[optind++];
				break;
			case 't' :
				op = 2;
				file = argv[optind++];
				break;
			case 'l' :
				location = argv[optind++];
				break;
			default :
				print_usage(argv[0]);
				exit(-1);
		}
	}

	if(host==NULL&&port>0) return run_server(port);
	else if(op<0&&conf!=NULL) return run_server(conf);
	else if(op>=0&&conf!=NULL) return run_client(conf,op,file, location);
	else return run_client(host,port,op,file, location);
}
